<?php


namespace App\HttpController;


use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Http\Message\Status;
use EasySwoole\VerifyCode\Conf;

class Welcome extends \App\Base\ViewController
{
    public function __construct()
    {
        parent::__construct();
        $this->model= new \App\Model\MemberModel();
    }

    public function index()
    {
        $this->response()->redirect("/welcome/login");
    }

    public function login(){
        $this->session()->start();
        $admin=$this->session()->get('ADMIN');
        if($admin){
            $this->response()->redirect("/frameset/index");
        }

        $request=$this->request();
        $post = $request->getParsedBody();
        if($post){
            $username=trim($post['username']);
            $password=trim($post['password']);
            $code=trim($post['code']);
            if(!$username or !$password){
                $this->msg(0,'账号和密码不能为空');
                return;
            }
            if(!$code){
                $this->msg(0,'验证码不能为空');
                return;
            }
            if(strtolower($code)!=strtolower($this->session()->get('CODE'))){
                $this->msg(0,'验证码不正确');
                return;
            }
            $user=$this->model->where(['username'=>$username])->get();
            if(!$user){
                $this->msg(0,'账号不存在');
                return;
            }
            if($user['groupid']!=1 and $user['groupid']!=2){
                $this->msg(0,'账号已经被禁用');
                return;
            }
            //密码 123456
            if(md5($password)!=$user['password']){
                $this->msg(0,'密码错误');
                return;
            }
            $login_back_model=new \App\Model\Login_backModel();
            $login_back_model->username=$username;
            $login_back_model->loginip=$this->clientRealIP();
            $login_back_model->logintime=time();
            $login_back_model->agent=$this->USERAGENT;
            $login_back_model->save();

            unset($user['password']);
            //session
            $this->session()->set('ADMIN',json_encode($user));
            $this->msg(1,'登录成功');
            return;
            //$this->response()->redirect("/frameset/index");
        }

        $this->fetch('login');
    }
}